/*
 * Decompiled with CFR 0.150.
 * 
 * Could not load the following classes:
 *  cn.hutool.core.util.ObjectUtil
 *  com.alibaba.fastjson.JSON
 *  de.felixroske.jfxsupport.FXMLController
 *  javafx.application.Platform
 *  javafx.collections.ObservableList
 *  javafx.event.EventHandler
 *  javafx.fxml.FXML
 *  javafx.fxml.Initializable
 *  javafx.scene.control.Alert
 *  javafx.scene.control.Alert$AlertType
 *  javafx.scene.control.TextField
 *  javafx.scene.input.KeyCode
 *  javafx.scene.input.KeyEvent
 *  javafx.scene.layout.Pane
 *  javafx.stage.Modality
 *  javafx.stage.Stage
 *  org.springframework.beans.factory.annotation.Autowired
 */
package com.gys.controller.salesReceipts;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.gys.common.business.SalesReceiptsUtil;
import com.gys.common.data.HttpJson;
import com.gys.common.util.CacheUtil;
import com.gys.common.util.CommonUtil;
import com.gys.common.util.SpringUtil;
import com.gys.controller.BaseController;
import com.gys.controller.dataManger.ControllerManger;
import com.gys.controller.salesReceipts.PointExchangeController;
import com.gys.controller.salesReceipts.QueryMemberTipController;
import com.gys.controller.salesReceipts.SalesReceiptsMainController;
import com.gys.data.example.GetUserOutData;
import com.gys.data.salesReceipts.GaiaSdMemberClass;
import com.gys.data.salesReceipts.GetPromByProCodeOutData;
import com.gys.data.salesReceipts.GetQueryMemberOutData;
import com.gys.data.salesReceipts.GetSalesReceiptsTableOutData;
import com.gys.view.salesReceipts.QueryMemberTipView;
import com.gys.view.salesReceipts.QueryMemberView;
import de.felixroske.jfxsupport.FXMLController;
import java.math.BigDecimal;
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.TimerTask;
import javafx.application.Platform;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import org.springframework.beans.factory.annotation.Autowired;

@FXMLController
public class QueryMemberController
extends BaseController
implements Initializable {
    @FXML
    private Pane queryMemberPane;
    @FXML
    private TextField phone;
    @FXML
    private TextField cardID;
    @FXML
    private TextField company;
    @Autowired
    private SalesReceiptsMainController salesReceiptsMainController;
    @Autowired
    private PointExchangeController pointExchangeController;
    @Autowired
    private QueryMemberTipController queryMemberTipController;
    private Timer timer = new Timer();
    private int enterConut = 0;
    private Boolean enterFlag = true;
    private GetQueryMemberOutData outData = null;

    public void initialize(URL location, ResourceBundle resources) {
        ControllerManger.controllerMap.put(this.getClass().getSimpleName(), this);
        this.init();
    }

    @Override
    public void init() {
        if (ObjectUtil.isEmpty(this.queryMemberPane)) {
            return;
        }
        this.phone.requestFocus();
        this.enterConut = 0;
        this.outData = null;
        this.phone.clear();
        this.cardID.clear();
        this.company.clear();
        this.timer.schedule(new TimerTask(){

            @Override
            public void run() {
                QueryMemberView view = SpringUtil.getBean(QueryMemberView.class);
                Stage stage = (Stage)view.getView().getScene().getWindow();
                stage.getScene().setOnKeyReleased((EventHandler)new EventHandler<KeyEvent>(){

                    public void handle(KeyEvent e) {
                        if (e.getCode() == KeyCode.ENTER) {
                            if (!QueryMemberController.this.enterFlag.booleanValue()) {
                                return;
                            }
                            if (!((Boolean)CacheUtil.get("networkFlag")).booleanValue()) {
                                QueryMemberController.this.sureAction();
                                return;
                            }
                            if (QueryMemberController.this.enterConut == 0) {
                                QueryMemberController.this.queryAction();
                            } else if (QueryMemberController.this.enterConut == 1) {
                                QueryMemberController.this.sureAction();
                            }
                        } else if (e.getCode() == KeyCode.LEFT) {
                            if (QueryMemberController.this.cardID.isFocused()) {
                                QueryMemberController.this.phone.requestFocus();
                            }
                        } else if (e.getCode() == KeyCode.RIGHT) {
                            if (QueryMemberController.this.phone.isFocused()) {
                                QueryMemberController.this.cardID.requestFocus();
                            }
                        } else if (e.getCode() == KeyCode.ESCAPE) {
                            QueryMemberController.this.cancelAction();
                        }
                        e.consume();
                    }
                });
            }
        }, 1000L);
    }

    private void enterFlagChange() {
        new Timer().schedule(new TimerTask(){

            @Override
            public void run() {
                QueryMemberController.this.enterFlag = true;
            }
        }, 500L);
    }

    @FXML
    private void queryAction() {
        if (!((Boolean)CacheUtil.get("networkFlag")).booleanValue()) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u5f53\u524d\u7f51\u7edc\u4e0d\u901a\uff0c\u8bf7\u8fdb\u884c\u79bb\u7ebf\u6a21\u5f0f\u6536\u94f6");
            alert.showAndWait();
            return;
        }
        String phoneVal = this.phone.getText().trim();
        String cardIDVal = this.cardID.getText().trim();
        if (ObjectUtil.isEmpty(phoneVal) && ObjectUtil.isEmpty(cardIDVal)) {
            this.enterFlag = false;
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u8bf7\u8f93\u5165\u624b\u673a\u53f7\u6216\u4f1a\u5458\u5361\u53f7");
            alert.showAndWait();
            this.enterFlagChange();
            return;
        }
        HashMap<String, Object> param = new HashMap<String, Object>();
        param.put("clientId", ((GetUserOutData)CacheUtil.get("logInUser")).getClient());
        param.put("phone", phoneVal);
        param.put("cardId", cardIDVal);
        HttpJson httpJson = SalesReceiptsUtil.queryMember(param);
        if (httpJson.isSuccess()) {
            this.outData = (GetQueryMemberOutData)JSON.parseObject((String)httpJson.getMsg(), GetQueryMemberOutData.class);
            if (ObjectUtil.isNotEmpty(this.outData)) {
                this.phone.setText(this.outData.getPhone());
                this.cardID.setText(this.outData.getCardNum());
                this.company.setText(this.outData.getCompany());
                this.enterConut = 1;
                Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u67e5\u8be2\u6210\u529f");
                alert.showAndWait();
            } else {
                this.cancelAction();
                Platform.runLater((Runnable)new Runnable(){

                    @Override
                    public void run() {
                        QueryMemberController.this.queryMemberTipController.init();
                        CommonUtil.showView(QueryMemberTipView.class, Modality.APPLICATION_MODAL);
                    }
                });
            }
        } else {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", httpJson.getMsg());
            alert.showAndWait();
        }
    }

    @FXML
    private void sureAction() {
        if (!((Boolean)CacheUtil.get("networkFlag")).booleanValue()) {
            String cardIDStr = this.cardID.getText().trim();
            if (ObjectUtil.isEmpty(cardIDStr)) {
                Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u8bf7\u8f93\u5165\u4f1a\u5458\u5361\u53f7");
                alert.showAndWait();
                return;
            }
            this.salesReceiptsMainController.getCardNum().setText(cardIDStr);
            GetQueryMemberOutData queryMemberOutData = new GetQueryMemberOutData();
            queryMemberOutData.setCardNum(cardIDStr);
            CacheUtil.put("memberOutData", queryMemberOutData);
            this.cancelAction();
            return;
        }
        if (ObjectUtil.isEmpty(this.outData)) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u8bf7\u67e5\u8be2\u4f1a\u5458");
            alert.showAndWait();
            return;
        }
        if (ObjectUtil.isEmpty(this.outData.getDis())) {
            Alert alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u672a\u8bbe\u7f6e\u4f1a\u5458\u5361\u7c7b\u578b\uff1a" + this.outData.getType() + " \u6298\u6263\uff0c\u6b64\u65f6\u9ed8\u8ba4\u4e0d\u6253\u6298");
            alert.showAndWait();
            this.outData.setDis("100");
        }
        CacheUtil.put("memberOutData", this.outData);
        this.salesReceiptsMainController.getCardNum().setText(this.outData.getCardNum());
        this.salesReceiptsMainController.getName().setText(this.outData.getName());
        this.salesReceiptsMainController.getPhone().setText(this.outData.getPhone());
        this.salesReceiptsMainController.getDis().setText(this.outData.getDis());
        this.salesReceiptsMainController.getType().setText(this.outData.getType());
        this.salesReceiptsMainController.getPoint().setText(this.outData.getPoint());
        this.salesReceiptsMainController.setTypeId(this.outData.getTypeId());
        this.pointExchangeController.init();
        this.count();
        this.cancelAction();
    }

    public void count() {
        Alert alert;
        HashSet<String> allC = new HashSet<String>();
        HashSet<String> proC = new HashSet<String>();
        for (int i = 0; i < this.salesReceiptsMainController.getTableView().getItems().size(); ++i) {
            GetSalesReceiptsTableOutData item = (GetSalesReceiptsTableOutData)this.salesReceiptsMainController.getTableView().getItems().get(i);
            if ("1".equals(item.getIsGift())) continue;
            if ("exclusion1".equals(item.getGsphExclusion())) {
                proC.add(item.getProCode());
                continue;
            }
            if (!"exclusion3".equals(item.getGsphExclusion())) continue;
            allC.add(item.getProCode());
        }
        ObservableList items = this.salesReceiptsMainController.getTableView().getItems();
        for (int i = 0; i < items.size(); ++i) {
            GetSalesReceiptsTableOutData item = (GetSalesReceiptsTableOutData)items.get(i);
            if (!"\u6b63\u5e38".equals(item.getStatus()) && !"\u4f1a\u5458\u5361\u6298\u6263".equals(item.getStatus())) continue;
            Float totalNum = Float.valueOf(this.salesReceiptsMainController.countSenglePro(item.getProCode()));
            BigDecimal totalMoney = new BigDecimal(totalNum.floatValue()).multiply(new BigDecimal(item.getPrcAmount()));
            HashMap<String, Object> param = new HashMap<String, Object>();
            param.put("nameOrCode", item.getProCode());
            GetQueryMemberOutData memberOutData = (GetQueryMemberOutData)CacheUtil.get("memberOutData");
            if (ObjectUtil.isNotEmpty(memberOutData)) {
                param.put("memberType", memberOutData.getTypeId());
            }
            param.put("totalNum", totalNum);
            param.put("num", this.salesReceiptsMainController.countSenglePro(item.getProCode()));
            param.put("totalMoney", totalMoney);
            param.put("proNumAmtMap", this.salesReceiptsMainController.proGroup());
            param.put("proIdNumMap", this.salesReceiptsMainController.proSingleLineGroup());
            param.put("allC", allC);
            param.put("proC", proC);
            HttpJson httpJson = SalesReceiptsUtil.memByProCode(param);
            if (httpJson.isSuccess()) {
                GetPromByProCodeOutData getPromByProCodeOutData = (GetPromByProCodeOutData)JSON.parseObject((String)httpJson.getMsg(), GetPromByProCodeOutData.class);
                if (ObjectUtil.isNotEmpty(getPromByProCodeOutData)) {
                    if (ObjectUtil.isNotEmpty(getPromByProCodeOutData.getDiscMem())) {
                        item.setDisc(getPromByProCodeOutData.getDiscMem());
                        float price = Float.valueOf(item.getPrcAmount()).floatValue() * Float.valueOf(getPromByProCodeOutData.getDiscMem()).floatValue() / 100.0f;
                        item.setPrice(CommonUtil.format2(price));
                        item.setTotal(CommonUtil.format2(price * Float.valueOf(item.getNum()).floatValue()));
                    } else if (ObjectUtil.isNotEmpty(getPromByProCodeOutData.getPriceMem())) {
                        item.setPrice(getPromByProCodeOutData.getPriceMem());
                        item.setDisc(String.valueOf((int)(100.0f * Float.valueOf(getPromByProCodeOutData.getPriceMem()).floatValue() / Float.valueOf(item.getPrcAmountD()).floatValue())));
                        item.setTotal(CommonUtil.format2(Float.valueOf(getPromByProCodeOutData.getPriceMem()).floatValue() * Float.valueOf(item.getNum()).floatValue()));
                    }
                    if ("1".equals(getPromByProCodeOutData.getMemberFlag())) {
                        item.setStatus("\u4f1a\u5458\u65e5\u7279\u4ef7");
                        continue;
                    }
                    if ("2".equals(getPromByProCodeOutData.getMemberFlag())) {
                        item.setStatus("\u4f1a\u5458\u65e5\u6298\u6263");
                        continue;
                    }
                    if (!"3".equals(getPromByProCodeOutData.getMemberFlag())) continue;
                    item.setStatus("\u4f1a\u5458\u4ef7");
                    continue;
                }
            } else {
                Alert alert2 = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", httpJson.getMsg());
                alert2.showAndWait();
                return;
            }
            item.setDisc(this.outData.getDis());
            float price = Float.valueOf(item.getPrcAmount()).floatValue() * Float.valueOf(this.outData.getDis()).floatValue() / 100.0f;
            item.setPrice(CommonUtil.format2(price));
            item.setTotal(CommonUtil.format2(price * Float.valueOf(item.getNum()).floatValue()));
            item.setStatus("\u4f1a\u5458\u5361\u6298\u6263");
        }
        this.salesReceiptsMainController.getTableView().refresh();
        HashMap<String, Object> param = new HashMap<String, Object>();
        GetQueryMemberOutData memberOutData = (GetQueryMemberOutData)CacheUtil.get("memberOutData");
        param.put("memberType", memberOutData.getTypeId());
        HttpJson pointSetHttpJson = SalesReceiptsUtil.getMemberPointSet(param);
        if (pointSetHttpJson.isSuccess()) {
            this.salesReceiptsMainController.gaiaSdMemberClass = (GaiaSdMemberClass)JSON.parseObject((String)pointSetHttpJson.getMsg(), GaiaSdMemberClass.class);
            if (ObjectUtil.isEmpty(this.salesReceiptsMainController.gaiaSdMemberClass)) {
                alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", "\u4f1a\u5458\u79ef\u5206\u8bbe\u7f6e\u4e3a\u7a7a,\u4e0d\u505a\u79ef\u5206");
                alert.showAndWait();
            }
        } else {
            alert = this.createAlert(Alert.AlertType.INFORMATION, "\u63d0\u793a\u4fe1\u606f", pointSetHttpJson.getMsg());
            alert.showAndWait();
            return;
        }
        this.salesReceiptsMainController.countMoney();
    }

    @FXML
    private void cancelAction() {
        this.queryMemberPane.getScene().getWindow().hide();
    }
}

